package com.itheima.d2_UDP;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketAddress;
import java.net.SocketException;
import java.lang.String;

public class Server {
    public static void main(String[] args) throws Exception {
        System.out.println("==启动服务端===");
        //1. 创建接受对象
        DatagramSocket socket_receiver = new DatagramSocket(8888);

        // 2. 创建数据包对象(盘子)
        byte[] bytes = new byte[1024 * 64];
        DatagramPacket packet = new DatagramPacket(bytes, bytes.length);
        //3 . 等待接受数据
        socket_receiver.receive(packet);
        int length = packet.getLength();

        //4. 取出数据
        String s = new String(bytes,0,length);
        System.out.println(s);

        //5. 获取发送端的ip，端口
        String address = packet.getSocketAddress().toString();
        int port = packet.getPort();
        System.out.println("对方地址"+address);
        System.out.println("对方端口"+port);
        socket_receiver.close();

    }
}
